import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import { Toast } from 'vant';
import store from '../store/index'

import home from '../components/tabbar/Home.vue'
import search from '../components/tabbar/Search.vue'
import mine from '../components/tabbar/Mine.vue'
import searchinfo from '../components/info/SearchInfo.vue' //搜索页
import issueinfo from '../components/info/IssueInfo.vue' //帖子发布页
import cardinfo from '../components/info/CardInfo.vue' //帖子详情页
import rewrite from '../components/info/Rewrite.vue' //修改帖子页


import login from '../components/login/Login.vue'//登陆
import registered from '../components/login/Registered.vue'//注册
import forget from '../components/login/Forget.vue'//忘记密码

import personalData from '../components/personal/PersonalData.vue'//个人信息
import updateInfo from '../components/personal/UpdateInfo.vue'//编辑信息
import partionlist from '../components/find/PartitonList.vue'//分类详情页

//
import { checkInfo } from '../api/forum-api'
// export default new Router({
const routes = [
  { path: '/', redirect: '/home' },
  { path: '/home', component: home },
  { path: '/search', component: search },
  // { path: '/mine', component: mine },
  { path: '/searchinfo', component: searchinfo },
  { path: '/issue', component: issueinfo },
  { path: '/cardinfo/:postsId', component: cardinfo },
  { path: '/', redirect: '/home' },
  // {path:'/home/:categoryId',component:home},
  // { path: '/search', component: search },
  {
    path: '/mine', component: mine,
    //路由元数据，表示这个页面是需要鉴权的，登陆以后才可以访问
    meta: {
      isAuth: true
    }
  },
  { path: '/login', component: login },
  { path: '/registered', component: registered },
  { path: '/forget', component: forget },
  { path: '/personalData', component: personalData },
  { path: '/updateInfo', component: updateInfo },
  { path: '/partionlist/:categoryId/:name', component: partionlist, name: 'partionlist' },
  { path: '/rewrite/:postsId/:title',component:rewrite,name:'rewrite'}
]
// })

//新建一个路由实例
const router = new Router({
  routes
})

//判断登陆状态要放在App.vue的生命周期函数中，因为已进入系统就需要对登陆状态进行判断
//loginPromise这里用的是promise的链式调用
let loginPromise = checkInfo().then(res => {
  // console.log(res.code)
  //判断登录状态
  if (res.code === 0) {
    //登陆成功
    store.commit("changeIsLogin", { isLogin: true })
    //请求用户信息

  } else {
    //登录失败
    store.commit("changeIsLogin", { isLogin: false })
  }
}).then(() => {
  //通过promise解决异步问题
  //执行到这里，说明上面的内容已经判断完成了

})
store.commit("changeLoginPromise", { loginPromise: loginPromise })



//添加一个路由守卫
router.beforeEach((to, from, next) => {
  // console.log(store)
  // 不写next代表拦截所有操作
  // console.log('我拦截了路由得操作');
  //判断登陆状态,判断页面是否需要鉴权，true的话需要登陆才能访问，否则的话可以直接访问
  // debugger
  if (to.meta.isAuth) {
    //这时判断登陆状态应该放在store的loginpromise里面判断
    store.state.loginPromise.then(() => {
      //已经请求到了登陆状态
      if (store.state.isLogin) {
        //登陆了
        next();
      } else {
        //未登录
        Toast('请先登陆');
        next('/login')
      }

    })


  } else {
    next();
  }
  // next()
})

//导出
export default router;